package cn.lf;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * 简易代码生成
 * @author guoyy
 */
public class FootBathCodeGen {

    public static void main(String[] args) {

        final String projectPath = System.getProperty("user.dir");

        AutoGenerator autoGenerator= new AutoGenerator();

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(projectPath+"/foot-bath-code-gen/src/main/java/generator");
        globalConfig.setAuthor("linf");
        globalConfig.setOpen(false);
        autoGenerator.setGlobalConfig(globalConfig);

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://106.54.48.49:3306/foot-bath-dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("Wp65Ecp6hPK4FHXX");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        autoGenerator.setDataSource(dataSourceConfig);

        final PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("foot-bath");
        packageConfig.setParent("cn.lf");
        autoGenerator.setPackageInfo(packageConfig);

        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        List<FileOutConfig> fileOutConfigList = new ArrayList<FileOutConfig>();

        final String projectPathFinal= projectPath;

        final PackageConfig packageConfigFinal= packageConfig;
        fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPathFinal+"/foot-bath-code-gen/src/main/resources/mapper"+packageConfigFinal.getModuleName()+"/"+
                        tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
            }
        });
        injectionConfig.setFileOutConfigList(fileOutConfigList);

        autoGenerator.setCfg(injectionConfig);

        autoGenerator.setTemplate(new TemplateConfig().setXml(null));

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setInclude("shop_info");
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setTablePrefix(packageConfig.getModuleName()+"_");

        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }

}
